<?php
	class Home extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			if (!isset($_SESSION)) {
			    session_start();
			    session_regenerate_id();
			}
			$this->load->helper("url");
			
		}
		public function index()
		{
			$data=array();
			
			$this->load->model("mcategory");
			$data['list_category']=$this->mcategory->list_category();
			
			$this->load->model("mdeal");
			$data['list_deal_top_100']=$this->mdeal->list_deal_top_100();
			
			$data['list_deal_top_seller']=$this->mdeal->list_deal_top_seller();
			
			$data['list_deal']=$this->mdeal->list_deal(0);
			
			$data['action']="home";
			$this->load->view("main_template", $data);
		}
		
		public function add_cart()
		{
			$array_cart=array();
			if(isset($_SESSION['cart']))
				$array_cart=$_SESSION['cart'];
				
			$id_deal=$_REQUEST['id_deal'];	
			$co=0;
			$list=array();
			foreach ($array_cart as $value =>$giatri){
				if($giatri[0]==$id_deal){
					$giatri[1]++;
					$co=1;
				}
				$list[]=array($giatri[0],$giatri[1],$giatri[2],$giatri[3],$giatri[4],$giatri[5],$giatri[6]);
			}
			$array_cart=$list;
			
			if($co==0){
			$this->load->model("mdeal");
			$data=$this->mdeal->get_deal($id_deal);
		
			$array_cart[]=array($id_deal,1,$data[0]['small_image'],$data[0]['name'],$data[0]['price'],$data[0]['sale_off'],$data[0]['price_sale_off']);
			}
			
			$_SESSION['cart']=$array_cart;
			echo count($array_cart);
		}
		
		
	}
?>